package astoliarskyi.lab5.conpool;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

class FixingIdleTimeConnection implements Connection {
	
	private static final String idleTimeoutExceed =
			"Connection idle time has exceeded max value.";
	
	private final long idleTime;	
	private Connection delegate;
	private long lastUsed;
	private final ConnectionPool owner;
	
	public FixingIdleTimeConnection(Connection connection,
			ConnectionPool owner, long idleTime){
		if (connection != null) 
			this.delegate = connection;
		else
			throw new IllegalArgumentException();
		this.owner = owner;
		this.idleTime = idleTime;
		fixUsageTime();
	}
	
	public synchronized Connection checkConnectionExpiration() {
		if ((System.currentTimeMillis() - lastUsed) > idleTime) {
			Connection connectionToReturn = delegate;
			delegate = null;
			return connectionToReturn;
		}
		return null;
	}
	
	public synchronized Connection getWrappedConnection() {
		return delegate;
	}
	
	private void fixUsageTime() {
		lastUsed = System.currentTimeMillis();
	}
	
	@Override
	public synchronized <T> T unwrap(Class<T> iface) throws SQLException {
		try {
			if ("java.sql.Connection".equals(iface.getName())
					|| "java.sql.Wrapper.class".equals(iface.getName())) {
				return iface.cast(this);
			}
			return delegate.unwrap(iface);
		} catch (ClassCastException cce) {
			throw new SQLException("Unable to unwrap to " + iface.toString(),
					cce);
		}
	}

	@Override
	public synchronized boolean isWrapperFor(Class<?> iface)
			throws SQLException {
		if ("java.sql.Connection".equals(iface.getName())
				|| "java.sql.Wrapper.class".equals(iface.getName())) {
			return true;
		}
		return delegate.isWrapperFor(iface);
	}

	@Override
	public synchronized Statement createStatement() throws SQLException {
		try {
			Statement toReturn = delegate.createStatement();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized PreparedStatement prepareStatement(String sql) throws SQLException {
		try {
			PreparedStatement toReturn = delegate.prepareStatement(sql);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized CallableStatement prepareCall(String sql) throws SQLException {
		try {
			CallableStatement toReturn = delegate.prepareCall(sql);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized String nativeSQL(String sql) throws SQLException {
		try {
			String toReturn = delegate.nativeSQL(sql);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setAutoCommit(boolean autoCommit) throws SQLException {
		try {
			delegate.setAutoCommit(autoCommit);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized boolean getAutoCommit() throws SQLException {
		try {
			boolean toReturn = delegate.getAutoCommit();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void commit() throws SQLException {
		try {
			delegate.commit();
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void rollback() throws SQLException {
		try {
			delegate.rollback();
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void close() throws SQLException {
		owner.checkin(this);
	}

	@Override
	public synchronized boolean isClosed() throws SQLException {
		try {
			boolean toReturn = delegate.isClosed();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized DatabaseMetaData getMetaData() throws SQLException {
		try {
			DatabaseMetaData toReturn = delegate.getMetaData();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setReadOnly(boolean readOnly) throws SQLException {
		try {
			delegate.setReadOnly(readOnly);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized boolean isReadOnly() throws SQLException {
		try {
			boolean toReturn = delegate.isReadOnly();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setCatalog(String catalog) throws SQLException {
		try {
			delegate.setCatalog(catalog);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized String getCatalog() throws SQLException {
		try {
			String toReturn = delegate.getCatalog();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setTransactionIsolation(int level) throws SQLException {
		try {
			delegate.setTransactionIsolation(level);;
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized int getTransactionIsolation() throws SQLException {
		try {
			int toReturn = delegate.getTransactionIsolation();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized SQLWarning getWarnings() throws SQLException {
		try {
			SQLWarning toReturn = delegate.getWarnings();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void clearWarnings() throws SQLException {
		try {
			delegate.clearWarnings();
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}	
	}

	@Override
	public synchronized Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		try {
			Statement toReturn = delegate.createStatement(resultSetType, resultSetConcurrency);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		try {
			PreparedStatement toReturn = delegate.prepareStatement(sql, resultSetType, resultSetConcurrency);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		try {
			CallableStatement toReturn = delegate.prepareCall(sql, resultSetType, resultSetConcurrency);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Map<String, Class<?>> getTypeMap() throws SQLException {
		try {
			Map<String, Class<?>> toReturn = delegate.getTypeMap();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setTypeMap(Map<String, Class<?>> map) throws SQLException {
		try {
			delegate.setTypeMap(map);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setHoldability(int holdability) throws SQLException {
		try {
			delegate.setHoldability(holdability);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized int getHoldability() throws SQLException {
		try {
			int toReturn = delegate.getHoldability();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Savepoint setSavepoint() throws SQLException {
		try {
			Savepoint toReturn = delegate.setSavepoint();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Savepoint setSavepoint(String name) throws SQLException {
		try {
			Savepoint toReturn = delegate.setSavepoint(name);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void rollback(Savepoint savepoint) throws SQLException {
		try {
			delegate.rollback(savepoint);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void releaseSavepoint(Savepoint savepoint) throws SQLException {
		try {
			delegate.releaseSavepoint(savepoint);
			fixUsageTime();
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Statement createStatement(int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		try {
			Statement toReturn = 
				delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized PreparedStatement prepareStatement(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		try {
			PreparedStatement toReturn = 
				delegate.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency, int resultSetHoldability)
			throws SQLException {
		try {
			CallableStatement toReturn = 
				delegate.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
			throws SQLException {
		try {
			PreparedStatement toReturn = 
				delegate.prepareStatement(sql, autoGeneratedKeys);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized PreparedStatement prepareStatement(String sql, int[] columnIndexes)
			throws SQLException {
		try {
			PreparedStatement toReturn = 
				delegate.prepareStatement(sql, columnIndexes);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized PreparedStatement prepareStatement(String sql, String[] columnNames)
			throws SQLException {
		try {
			PreparedStatement toReturn = 
				delegate.prepareStatement(sql, columnNames);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Clob createClob() throws SQLException {
		try {
			Clob toReturn = 
				delegate.createClob();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Blob createBlob() throws SQLException {
		try {
			Blob toReturn = 
				delegate.createBlob();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized NClob createNClob() throws SQLException {
		try {
			NClob toReturn = 
				delegate.createNClob();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized SQLXML createSQLXML() throws SQLException {
		try {
			SQLXML toReturn = 
				delegate.createSQLXML();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized boolean isValid(int timeout) throws SQLException {
		try {
			boolean toReturn = 
				delegate.isValid(timeout);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setClientInfo(String name, String value)
			throws SQLClientInfoException {
		delegate.setClientInfo(name, value);
		fixUsageTime();
	}

	@Override
	public synchronized void setClientInfo(Properties properties)
			throws SQLClientInfoException {
		delegate.setClientInfo(properties);
		fixUsageTime();
	}

	@Override
	public synchronized String getClientInfo(String name) throws SQLException {
		try {
			String toReturn = 
				delegate.getClientInfo(name);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Properties getClientInfo() throws SQLException {
		try {
			Properties toReturn = 
				delegate.getClientInfo();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Array createArrayOf(String typeName, Object[] elements)
			throws SQLException {
		try {
			Array toReturn = 
				delegate.createArrayOf(typeName, elements);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized Struct createStruct(String typeName, Object[] attributes)
			throws SQLException {
		try {
			Struct toReturn =
				delegate.createStruct(typeName, attributes);
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void setSchema(String schema) throws SQLException {
		delegate.setSchema(schema);
		fixUsageTime();
	}

	@Override
	public synchronized String getSchema() throws SQLException {
		try {
			String toReturn =
				delegate.getSchema();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}

	@Override
	public synchronized void abort(Executor executor) throws SQLException {
		delegate.abort(executor);
		fixUsageTime();
	}

	@Override
	public synchronized void setNetworkTimeout(Executor executor, int milliseconds)
			throws SQLException {
		delegate.setNetworkTimeout(executor, milliseconds);
		fixUsageTime();
	}

	@Override
	public synchronized int getNetworkTimeout() throws SQLException {
		try {
			int toReturn =
				delegate.getNetworkTimeout();
			fixUsageTime();
			return toReturn;
		} catch (NullPointerException e) {
			throw new SQLException(idleTimeoutExceed);
		}
	}
    
}
